# -*- coding: utf8 -*-

from plugmgr import plugin
from core import __version__
import twisted, twisted.words, sys, popen2

__all__ = ["testplugin", "echo", "info",'eventsTest']

class testplugin(plugin):
    requires = ["cmd", "lang"]
    def __init__(self, bot):
        self.bot = bot
        # добавляем лангпак "test"
        bot["lang"].add("test")
        # регистрируем команду "test"
        bot["cmd"].addHandler("test", self.handleTest)
    def unload(self):
        self.bot["lang"].remove("test")
        self.bot["cmd"].removeHandler("test")
    def handleTest(self, c):
        """
        @debug
        Simple test command. Bot replies "passed".
        Example: %cmd_prefix%test
        """
        # тут используем перевод из подключенного выше лангпака.
        c.reply(c._("Passed."))

class echo(plugin):
    requires = ["cmd"]
    def __init__(self, bot):
        self.bot = bot
        bot["cmd"].addHandler("echo", self.echo)
    def echo(self, c):
        c.reply(c.args)
    def unload(self):
        self.bot["cmd"].removeHandler("echo")

class info(plugin):
    requires = ['cmd']
    def __init__(self,bot):
        self.bot = bot
        self.bot["cmd"].addHandler("info", self.info)
        self.bot["cmd"].addHandler("about", self.about)
    def info(self,c):
        c.reply(c._('Your access level is %s')%c.access())
    def about(self,c):
        v = __version__
        try:    r = popen2.Popen3('svn info | grep -i revision | grep -o "[0-9]*"').fromchild.readline()[:-1]
        except: r = '<non-svn>'
        tww = twisted.words.__version__
        tw = twisted.__version__
        py = sys.version.replace('\n',' ')
        c.reply('Eggrevenger %s (r%s)\nTwisted words %s\nTwisted %s\nPython %s'%(v,r,tww,tw,py))
    def unload(self):
        self.bot["cmd"].removeHandler("info")
        self.bot["cmd"].removeHandler("about")

class eventsTest(plugin):
    requires = ['events','stanzas']
    def handler(self,source,type,body,lang):
        if body.find('O_o')!=-1:
            self.bot['stanzas'].sendMessage(type,source.split('/')[0],'o_O') # Не обращаться по нику
        elif body.find('censored')!=-1:
            return source,type,body.replace('censored','@#$%@#$%'),lang
        elif body.find('foo')!=-1:
            self.bot['stanzas'].sendMessage(type,source,'bar')
            raise StopIteration
    def __init__(self,bot):
        self.bot = bot
        self.handlerId = self.bot['events'].registerHandler('message',self.handler,True)
    def unload(self):
        self.bot['events'].removeHandler('message',self.handlerId)

